﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microhard.Protal.Models;
using System.Web.Mvc;
using Microhard.Protal.Interfaces;
using Microhard.Core;
using Microhard.Permission.Models;
using Microhard.Permission.Interface;


namespace Microhard.WebUI.Protal.Extensions
{
    public static class HtmlExtensions
    {
        public static List<DictionaryModel> GetDics(this HtmlHelper helper, string type)
        {
            IDictionary cat = IoC.Resolve<IDictionary>();
            var data = cat.Query(new DictionaryModel { Type = type });
            return data;
        }

        /// <summary>
        /// 门户类型选择
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static List<CategoryModel> GetCategoryRoot(this HtmlHelper helper, Guid companyID)
        {
            ICategory cat = IoC.Resolve<ICategory>();
            var data = cat.GetRootCategories(null, companyID);
            data.Insert(0, new CategoryModel { Name = "无", CategoryID = Guid.Empty });
            return data;
        }

        /// <summary>
        /// 门户类型菜单
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static CategoryModel GetSingleCategoryMenu(this HtmlHelper helper, string name, Guid companyID)
        {
            ICategory cat = IoC.Resolve<ICategory>();
            var first = cat.GetRootCategories(name, companyID);
            if (first.Count > 0)
                return first.First();
            return new CategoryModel();
        }

        /// <summary>
        /// 获得所有二级菜单
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static List<CategoryModel> GetSecondSubCategoryMenus(this HtmlHelper helper, Guid companyID)
        {
            ICategory cat = IoC.Resolve<ICategory>();
            var data = cat.GetRootCategories(null, companyID);
            List<CategoryModel> result = new List<CategoryModel>();
            data.ForEach(item =>
            {
                if (item.Subs.Count > 0)
                {
                    result.AddRange(item.Subs);
                }
            });
            return result;
        }

        public static SiteInfoModel GetSiteInfo(this HtmlHelper helper, string keyName, Guid companyID)
        {
            ISiteInfo isi = IoC.Resolve<ISiteInfo>();
            List<SiteInfoModel> info = isi.Query(new SiteInfoModel { CompanyID = companyID });
            var siteInfo = helper.ViewContext.HttpContext.Session["SiteInfo"] as List<SiteInfoModel>;
            if (siteInfo == null)
            {
                helper.ViewContext.HttpContext.Session["SiteInfo"] = info;
                siteInfo = info;
            }
            var si = siteInfo.SingleOrDefault(s => s.Key.ToLower() == keyName.ToLower());
            if (si == null)
                return new SiteInfoModel { Key = keyName, Value = keyName };
            return si;
        }
    }
}